package com.example.tables.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.tables.bean.TableABean;
import com.example.tables.bean.TableBBean;
import com.example.tables.bean.TableCBean;
import com.example.tables.service.TableCService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tablec")
public class TableCController {
    @Autowired
    TableCService tableCService;
    @RequestMapping("/get")
    public Object getLike(@RequestParam(value = "b")String b){
        Map<String,Object> map =new HashMap<>();
        List<TableCBean> data = tableCService.getData(b);
        int i = data.size();
        if(data!= null || data.size()<0){
            map.put("code",200);
            map.put("msg", "success!");
            map.put("size", i);
            map.put("data", data);
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }

        return map;
    }

    //a表中aa总数
    @RequestMapping("/numa")
    public int numaaina(){
        List<TableABean> data = tableCService.numaainTa();
        return data.size();
    }
    //a表中bb总数
    @RequestMapping("/numb")
    public int numbbina(){
        List<TableABean> data = tableCService.numbbinTa();
        return data.size();
    }

    @RequestMapping("/numc")
    public int numccina(){
        List<TableABean> data = tableCService.numccinTa();
        return data.size();
    }

    @RequestMapping("/numaR")
    public int numaaRight(){
        List<TableBBean> data = tableCService.numaRight();
        return data.size();
    }

    @RequestMapping("/numbR")
    public int numbbRight(){
        List<TableBBean> data = tableCService.numbRight();
        return data.size();
    }

    @RequestMapping("/numcR")
    public int numccRight(){
        List<TableBBean> data = tableCService.numcRight();
        return data.size();
    }

    @RequestMapping("/numainb")
    public int numaainTb(){
        List<TableBBean> data = tableCService.numaainTb();
        return data.size();
    }

    @RequestMapping("/numbinb")
    public int numbbinTb(){
        List<TableBBean> data = tableCService.numbbinTb();
        return data.size();
    }

    @RequestMapping("/numcinb")
    public int numccinTb(){
        List<TableBBean> data = tableCService.numccinTb();
        return data.size();
    }

    @RequestMapping("/numeina")
    public int numeeina(){
        List<TableABean> data = tableCService.numeeinTa();
        return data.size();
    }

    @RequestMapping("/numeR")
    public int numeeRight(){
        List<TableBBean> data = tableCService.numeRight();
        return data.size();
    }

    @RequestMapping("/numdina")
    public int numddina(){
        List<TableABean> data = tableCService.numddinTa();
        return data.size();
    }

    @RequestMapping("/numdR")
    public int numddRight(){
        List<TableBBean> data = tableCService.numdRight();
        return data.size();
    }

    @RequestMapping("/calculate")
    public Object addData(@RequestParam(value = "b")String b){
        Map<String,Object> map =new HashMap<>();
        TableCBean tableCBean = new TableCBean();

        int aaS = numaaRight()/numaaina();
        int bbS = numbbRight()/numbbina();
        int ccS = numccRight()/numccina();
        int aaA = numaaRight()/numaainTb();
        int bbA = numbbRight()/numbbinTb();
        int ccA = numccRight()/numccinTb();
        int aaSS = numeeRight()/numeeina();
        int aaC = (numaaRight()+numeeRight())/(numaaina()+numeeina());
        int ddS = numddRight()/numddina();
        tableCBean.setB(b);
        tableCBean.setAaS(aaS);
        tableCBean.setBbS(bbS);
        tableCBean.setCcS(ccS);
        tableCBean.setAaA(aaA);
        tableCBean.setBbA(bbA);
        tableCBean.setCcA(ccA);
        tableCBean.setAaSS(aaSS);
        tableCBean.setAaC(aaC);
        tableCBean.setDdS(ddS);


        int check =tableCService.calculate(tableCBean);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }

        return map;
    }

    @RequestMapping("/getall")
    public List<TableCBean> getAll(){
        List<TableCBean> data  = tableCService.getAll();

        return data;
    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response)throws IOException {
        List<TableCBean> list = getAll();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("tablec","utf-8").replaceAll("\\+","%20");
        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");

        EasyExcel.write(response.getOutputStream())
                .head(TableCBean.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(1)
                .doWrite(list);
    }


}
